package com.sfhk.web;

import com.sfhk.domain.User;
import com.sfhk.service.UserService;
import com.sfhk.service.impl.UserServiceImpl;
import com.sfhk.utils.LoginException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.接收请求
        request.setCharacterEncoding("UTF-8");

        String username = null;
        String password = null;
        username = request.getParameter("username");
        password = request.getParameter("password");
        String check = request.getParameter("verifycode");
        Object cWord = request.getSession().getAttribute("checkcode_session");

//        Cookie[] cookies = request.getCookies();//获取cookie
//        if(cookies!=null) {//如果获取到了
//            for (Cookie c : cookies) {//遍历
//                //将登录时保存的用户名和密码拿出来，并将flag2设置为true
//                if (c.getName().equals("username")) {
//                    username = c.getValue();
//                } else if (c.getName().equals("password")) {
//                    password = c.getValue();
//                }
//                if (username != null && password != null) {
//                    username = request.getParameter("username");
//                    password = request.getParameter("password");
//                    break;
//                }
//            }
//        }

        String remember = request.getParameter("remember");
        String remembername = request.getParameter("remembername");


        // 2.调用service的方法
        if (check.equals(cWord)) {
            try {
                User loginuser = userService.login(username, password);
                if("remembername".equals(remembername)){
                    request.getSession().setAttribute("logUsername", username);
                    request.getSession().setAttribute("remembername", remembername);
                }
                if("remember".equals(remember)){//如果选中了自动登录
                    HttpSession session = request.getSession();
                    String uri = request.getContextPath() + "/";//存储路径
                    int expiry = 60*60*24*15;//存储时间
                    Cookie c = new Cookie("username",username);//存储内容
                    c.setPath(uri);
                    c.setMaxAge(expiry);
                    response.addCookie(c);//添加到浏览器的Cookie
                    c = new Cookie("password", password);
                    c.setPath(uri);
                    c.setMaxAge(expiry);
                    response.addCookie(c);
                    c = new Cookie("JSESSIONID",session.getId());
                    c.setPath(uri);
                    c.setMaxAge(expiry);
                    response.addCookie(c);
                    request.getSession().setAttribute("remember", remember);
                    request.getSession().setAttribute("logUsername", username);
                    request.getSession().setAttribute("loginuser", loginuser);
                }
                request.getSession().setAttribute("user",loginuser);
                response.sendRedirect(request.getContextPath());
            } catch (LoginException e) {
                request.setAttribute("errorMessage", e.getMessage());
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }
            // 3.request域传递数据

            // 4.转发到jsp中
        }else {
            request.setAttribute("errorMessage", "验证码错误");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
